package edu.whu.irlab.util.time;

import org.joda.time.DateTime;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Timer {
    static final String r1 = "(\\d+)\\s+([A-Za-z]{3,4})+\\,\\s*(\\d{4})"; //1 Oct,2013
    static final String r2 = "(\\d{1,2})月(\\d{1,2})日（.）(\\d{1,2})時(\\d{1,2})分";//10月10日（木） 11時0分 雅虎新闻日文搜索
    static final String r4 = "(\\d{4})年(\\d{1,2})月(\\d{1,2})日（.）(\\d{1,2})時(\\d{1,2})分";  //2012年11月10日（土） 17時9分
    static final String r5 = "([A-Za-z]{3,4})\\s+(\\d{1,2})\\,\\s*(\\d{4})"; // Apr 10, 2010
    static final String r6 = "(\\d{4})-(\\d{1,2})-(\\d{1,2})\\s*(\\d{1,2})*\\:*(\\d{1,2})*\\:*(\\d{1,2})*";    //2012-11-11 23:32:02
    static final String r7 = "([A-Za-z]{3})\\s?(\\d{2})\\s?(\\d{2})\\:(\\d{2})am";//针对雅虎新闻英文搜索 (am：上午)
    static final String r8 = "([A-Za-z]{3})\\s?(\\d{2})\\s?(\\d{2})\\:(\\d{2})pm";//针对雅虎新闻英文搜索 (pm：上午)
    static final String r9 = "(\\d{1,2})\\s+hours?,\\s+(\\d{1,2})\\s+minutes? ago";//针对雅虎新闻英文搜索 (坑爹的m小时n分钟之前)
    static final String r10 = "(\\d{1,2})小时前";
    static final String r11 = "(\\d{4})年\\d{1,2}月\\d{1,2}日 (\\d{1,2}):(\\d{1,2})";
    static final String r12 = "(\\d{4})-(\\d{1,2})-(\\d{1,2})";
    static final String r13 = "(\\d{1,2})月(\\d{1,2})日 (\\d{1,2}):(\\d{1,2})";
    static final String r14 = "(\\d{4})-(\\d{1,2})-(\\d{1,2}) (\\d{1,2}):(\\d{1,2}):(\\d{1,2})";
    static final String r15 = "(\\d{1,2}):(\\d{1,2})";
    static final String r16 = "(\\d{1,2})月(\\d{1,2})日";
    static final String r17 = "(\\d{1,3})分钟前";
    static final String r18 = "今天 (\\d{1,2}):(\\d{1,2})";
    static final String r19 = "昨天 (\\d{1,2}):(\\d{1,2})";

    public static Timestamp standard(String date) {
        if (date == null) {
            return null;
        }
        date = date.trim();
        Timestamp result;
        DateStandardizer matcher = new PatternStandardizer(r6, new int[]{1, 2, 3, 4, 5, 6});
        result = matcher.get(date);
        if (result != null) {
            return result;
        }
        //首先按照偏移进行匹配
        matcher = new ShiftStandardizer(new int[]{1, 2}, new Date());
        result = matcher.get(date);
        if (result != null) {
            return result;
        }
        matcher = new PatternStandardizer(r1, new int[]{3, 2, 1, 0, 0, 0});
        result = matcher.get(date);
        if (result != null) {
            return result;
        }
        matcher = new PatternStandardizer(r2, new int[]{0, 1, 2, 3, 4, 0});
        result = matcher.get(date);
        if (result != null) {
            return result;
        }
        matcher = new PatternStandardizer(r4, new int[]{1, 2, 3, 4, 5, 0});
        result = matcher.get(date);
        if (result != null) {
            return result;
        }
        matcher = new PatternStandardizer(r5, new int[]{3, 1, 2, 0, 0, 0});
        result = matcher.get(date);
        if (result != null) {
            return result;
        }
        matcher = new PatternStandardizer(r7, new int[]{0, 1, 2, 3, 4, 0});
        result = matcher.get(date);
        if (result != null) {
            return result;
        }
        matcher = new PatternStandardizer(r8, new int[]{0, 1, 2, 3, 4, 0});
        result = matcher.get(date);
        if (result != null) {
            result.setTime(result.getTime() + 12 * 3600 * 1000);//pm时间，加上12个小时转换成24小时制
            return result;
        }
        Pattern p = Pattern.compile(r9);
        Matcher m = p.matcher(date);
        if (m.matches()) {
            int hour = Integer.parseInt(m.group(1));
            int minutes = Integer.parseInt(m.group(2));
            Date date1 = new Date();
            long nowTime = date1.getTime();
            long newsTime = nowTime - (hour * 3600 * 1000 + minutes * 60 * 1000);
            result = new Timestamp(newsTime);
            return result;
        }

        Pattern p10 = Pattern.compile(r10);
        Matcher m10 = p10.matcher(date);
        if (m10.matches()) {
            String shift = m10.group(1);
            DateTime dateTime = new DateTime();
            dateTime.minusHours(Integer.parseInt(shift));
            result = new Timestamp(dateTime.getMillis());
            return result;
        }

        Pattern p11 = Pattern.compile(r11);
        Matcher m11 = p11.matcher(date);
        if (m11.find()) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
            try {
                Date date1 = simpleDateFormat.parse(date);
                result = new Timestamp(date1.getTime());
            } catch (ParseException e) {
                e.printStackTrace();
            }
            return result;
        }

        Pattern p12 = Pattern.compile(r12);
        Matcher m12 = p12.matcher(date);
        if (m12.find()) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            try {
                Date date1 = simpleDateFormat.parse(date);
                result = new Timestamp(date1.getTime());
            } catch (ParseException e) {
                e.printStackTrace();
            }
            return result;
        }

        Pattern p13 = Pattern.compile(r13);
        Matcher m13 = p13.matcher(date);
        if (m13.find()) {
            int month = Integer.parseInt(m13.group(1));
            int day = Integer.parseInt(m13.group(2));
            int hour = Integer.parseInt(m13.group(3));
            int minute = Integer.parseInt(m13.group(4));
            Date date1 = new Date();
            date1.setMonth(month - 1);
            date1.setDate(day);
            date1.setHours(hour);
            date1.setMinutes(minute);
            result = new Timestamp(date1.getTime());
            return result;
        }

        Pattern p14 = Pattern.compile(r14);
        Matcher m14 = p14.matcher(date);
        if (m14.find()) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try {
                Date date1 = simpleDateFormat.parse(date);
                result = new Timestamp(date1.getTime());
            } catch (ParseException e) {
                e.printStackTrace();
            }
            return result;
        }

        Pattern p15 = Pattern.compile(r15);
        Matcher m15 = p15.matcher(date);
        if (m15.find()) {
            int hour = Integer.parseInt(m15.group(1));
            int minute = Integer.parseInt(m15.group(2));
            Date date1 = new Date();
            date1.setHours(hour);
            date1.setMinutes(minute);
            result = new Timestamp(date1.getTime());
            return result;
        }

        Pattern p16 = Pattern.compile(r16);
        Matcher m16 = p16.matcher(date);
        if (m16.find()) {
            int month = Integer.parseInt(m16.group(1));
            int day = Integer.parseInt(m16.group(2));
            Date date1 = new Date();
            date1.setMonth(month - 1);
            date1.setDate(day);
            result = new Timestamp(date1.getTime());
            return result;
        }

        Pattern p17 = Pattern.compile(r17);
        Matcher m17 = p17.matcher(date);
        if (m17.find()) {
            int minutes = Integer.parseInt(m17.group(1));
            DateTime dateTime = new DateTime();
            result = new Timestamp(dateTime.minusMinutes(minutes).getMillis());
            return result;
        }

        Pattern p18 = Pattern.compile(r18);
        Matcher m18 = p18.matcher(date);
        if (m18.find()) {
            int hour = Integer.parseInt(m18.group(1));
            int minute = Integer.parseInt(m18.group(2));
            Date date1 = new Date();
            date1.setHours(hour);
            date1.setMinutes(minute);
            result = new Timestamp(date1.getTime());
            return result;
        }

        Pattern p19 = Pattern.compile(r19);
        Matcher m19 = p19.matcher(date);
        if (m19.find()) {
            int hour = Integer.parseInt(m19.group(1));
            int minute = Integer.parseInt(m19.group(2));
            Date date1 = new Date();
            date1.setHours(hour);
            date1.setMinutes(minute);
            DateTime dateTime = new DateTime(date1.getTime());
            result = new Timestamp(dateTime.minusDays(1).getMillis());
            return result;
        }

        return null;
    }

    public static void main(String[] args) {
        Pattern p15 = Pattern.compile(r15);
        Matcher m15 = p15.matcher("12:01");
        if (m15.matches()) {
            int hour = Integer.parseInt(m15.group(1));
            int minute = Integer.parseInt(m15.group(2));
            Date date1 = new Date();
            date1.setHours(hour);
            date1.setMinutes(minute);
            System.out.println(new Timestamp(date1.getTime()));
        }
    }

//    public static void main(String[] args) {
//        for (int i = 0; i < 10000; i++) {
//            new Thread(new Runnable() {
//                @Override
//                public void run() {
////                    String date = "2014-05-13 01:20:51";
//                    String time="3小时前";
//                    Date d = TimeStandardizer.standard(time);
//                    if (d != null) {
//                        System.out.println(TimeUtils.date2DetailStr(d));
//                    }
//
//                }
//            }).start();
//
//        }
//    }
}
